﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OptimumInfra.ViewData;
namespace OptimumInfra.Models
{
    public class CustomerRepository
    {
        private OptimumDataContext op = new OptimumDataContext();
        public List<ViewDataCustomer> GetAllCustomers(bool employee)
        {
            //var result1 = new
            if (!employee)
            {
                var result = from c in op.Customers
                             join u in op.aspnet_Users on c.UserName equals u.UserName
                             select new { c.Id, c.UserName, c.PhoneNumber };
                List<ViewDataCustomer> t = new List<ViewDataCustomer>();
                foreach (var r in result)
                {
                    ViewDataCustomer temp = new ViewDataCustomer();
                    temp.Id = r.Id;
                    temp.UserName = r.UserName;
                    t.Add(temp);
                }
                return t;
            }
            else
            {
                var result = from c in op.Customers
                             join u in op.aspnet_Users on c.UserName equals u.UserName
                             join ur in op.aspnet_UsersInRoles on u.UserId equals ur.UserId
                             join r in op.aspnet_Roles on ur.RoleId equals r.RoleId
                             where r.RoleName == "admin"
                             select new { c.UserName, c.Id, c.Email, c.PhoneNumber, c.Address, r.RoleName, r.RoleId };
                List<ViewDataCustomer> t = new List<ViewDataCustomer>();
                foreach (var r in result)
                {
                    ViewDataCustomer temp = new ViewDataCustomer();
                    temp.Id = r.Id;
                    temp.UserName = r.UserName;
                    t.Add(temp);
                }
                return t;
            }
         
        }
        
        public void CreateCustomer(Customer c)
        {
            op.Customers.InsertOnSubmit(c);
            op.SubmitChanges();
        }
        public Customer GetCustomerByName(string userName)
        {
            return op.Customers.SingleOrDefault(a => a.UserName == userName);
        }
        /*
        public ViewDataCustomer GetViewDataCustomerByName(string userName)
        {
            var result = from c in op.Customers
                         join u in op.aspnet_Users on c.FirstName equals u.UserName
                        
                         where c.FirstName == userName
                         select new { c.FirstName, c.Id, u.pa c.Email, c.PhoneNumber, c.Address };
            ViewDataCustomer customer = new ViewDataCustomer();
            foreach (var r in result)
            {
                customer.Id = r.Id; ;
                customer.UserName = r.FirstName;
                customer.Email = r.Email;
                customer.PhoneNumber = r.PhoneNumber;
                customer.Address = r.Address;
            }
            return customer;
        }
         */

        public ViewDataCustomer GetCustomerById(int id)
        {
            var result = from c in op.Customers
                    join u in op.aspnet_Users on c.UserName equals u.UserName
                    join ur in op.aspnet_UsersInRoles on u.UserId equals ur.UserId
                    join r in op.aspnet_Roles on ur.RoleId equals r.RoleId
                    where c.Id == id
                    select new { c.UserName, c.Id, c.Email, c.PhoneNumber, c.Address, r.RoleName, r.RoleId };
            ViewDataCustomer customer = new ViewDataCustomer();
            foreach (var r in result)
            {
                customer.Id = r.Id; ;
                customer.UserName = r.UserName;
                customer.Email = r.Email;
                customer.Role = r.RoleName;
                customer.PhoneNumber = r.PhoneNumber;
                customer.Address = r.Address;            
            }
              return customer;
           
        }
        public Order CreateOrder(int CusomterId)
        {
            Order o = new Order();
            o.CustomerId = CusomterId;
            o.State = 1;
            this.op.Orders.InsertOnSubmit(o);
            op.SubmitChanges();
            return o;
        }
        public void DeleteCustomerById(int id)
        {
            var r = op.Customers.SingleOrDefault(a => a.Id == id);
            var c = op.aspnet_Users.SingleOrDefault(a => a.UserName == r.FirstName);
            var t = op.aspnet_UsersInRoles.SingleOrDefault(a => a.UserId == c.UserId);
            var m = op.aspnet_Memberships.SingleOrDefault(a => a.UserId == c.UserId);
            op.Customers.DeleteOnSubmit(r);
            op.aspnet_UsersInRoles.DeleteOnSubmit(t);
            op.aspnet_Memberships.DeleteOnSubmit(m);
            op.aspnet_Users.DeleteOnSubmit(c);
            op.SubmitChanges();
        }
        public void UpdateCustomer(ViewDataCustomer c)
        {
            var customer = op.Customers.SingleOrDefault(a => a.Id == c.Id);
            customer.FirstName = c.UserName;
            customer.Email = c.Email;
            customer.PhoneNumber = c.PhoneNumber;
            customer.Address = c.Address;
            //update role
            op.SubmitChanges();
        }
        public OrderProduct CreateOrderProduct(OrderProduct oProduct)
        {
            this.op.OrderProducts.InsertOnSubmit(oProduct);
            op.SubmitChanges();
            return oProduct;
        }
        public List<ViewDataOrder> GetOrdersByCustomer(int customerId)
        {
            var result = from r in op.Orders
                         join c in op.Customers on r.CustomerId equals c.Id
                         where r.CustomerId == customerId
                         select new { r.Id, r.DateAdd, r.State, r.CustomerId, r.DateUpdate, c.LastName, c.FirstName };

            List<ViewDataOrder> t = new List<ViewDataOrder>();
            foreach (var r in result)
            {
                ViewDataOrder temp = new ViewDataOrder();
                temp.Id = r.Id;
                temp.FirstName = r.FirstName;
                temp.Status = OrderStatus(r.State);
                t.Add(temp);
            }
            return t;

        }
        public string OrderStatus(int status)
        {
            string result = "Unknown";
            if (status == 1)
                result = "In progress";
            else if (status == 2)
                result = "Paid";
            return result;
        }
        public void GetCustomIt(int customerId)
        {
            var result = from r in op.Orders
                         join c in op.Customers on r.CustomerId equals c.Id
                         where r.CustomerId == customerId
                         select new { r.Id, r.DateAdd, r.State, r.CustomerId, r.DateUpdate, c.LastName, c.FirstName };

            List<ViewDataOrder> t = new List<ViewDataOrder>();
            foreach (var r in result)
            {
                ViewDataOrder temp = new ViewDataOrder();
                temp.Id = r.Id;
                temp.FirstName = r.FirstName;
                temp.Status = OrderStatus(r.State);
                t.Add(temp);
            }
        }
        public int GetActiveServer(int customerId)
        {
            int result = 0;
            result = (from r in op.Orders
                             join c in op.Customers on r.CustomerId equals c.Id
                             join oProduct in op.OrderProducts on r.Id equals oProduct.OrderId
                             where r.CustomerId == customerId && oProduct.ProductType == 1
                             select r.Id).Count();
            return result;
        }
        public int GetUsedCPU(int customerId)
        {
            int result = 0;
            result = (from r in op.Orders
                      join c in op.Customers on r.CustomerId equals c.Id
                      join oProduct in op.OrderProducts on r.Id equals oProduct.OrderId
                      where r.CustomerId == customerId && oProduct.ProductName =="CPU"
                      select r.Id).Count();
            return result;
        }
        public int GetUsedRAM(int customerId)
        {
            int result = 0;
            result = (from r in op.Orders
                      join c in op.Customers on r.CustomerId equals c.Id
                      join oProduct in op.OrderProducts on r.Id equals oProduct.OrderId
                      where r.CustomerId == customerId && oProduct.ProductName == "RAM"
                      select r.Id).Count();
            return result;
        }
        public int GetUsedStorage(int customerId)
        {
            int result = 0;
            result = (from r in op.Orders
                      join c in op.Customers on r.CustomerId equals c.Id
                      join oProduct in op.OrderProducts on r.Id equals oProduct.OrderId
                      where r.CustomerId == customerId && oProduct.ProductName == "Storage"
                      select r.Id).Count();
            return result;
        }
        public int GetUsedPublicIP(int customerId)
        {
            int result = 0;
            result = (from r in op.Orders
                      join c in op.Customers on r.CustomerId equals c.Id
                      join oProduct in op.OrderProducts on r.Id equals oProduct.OrderId
                      where r.CustomerId == customerId && oProduct.ProductName == "Public IP"
                      select r.Id).Count();
            return result;
        }
        public int GetUsedBandwith(int customerId)
        {
            int result = 0;
            result = (from r in op.Orders
                      join c in op.Customers on r.CustomerId equals c.Id
                      join oProduct in op.OrderProducts on r.Id equals oProduct.OrderId
                      where r.CustomerId == customerId && oProduct.ProductName == "Bandwith"
                      select r.Id).Count();
            return result;
        }
    }
}